import Vue from 'vue';
import VueRouter from 'vue-router';
// import store from './../store'
import routes from './router';
import { ys_utils } from 'ys-utils-js';
import { Message } from "view-design";

// Vue.use(VueRouter);

// // 解决重复定位同一路由报错
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location) {
//     return originalPush.call(this, location).catch(err => err);
// };

// const router = new VueRouter({
//     mode: window.GlobalConfig.IsOpenHistory ? 'history' : 'hash',
//     base: window.GlobalConfig.RouterBase || '',
//     routes,
// });

Vue.use(VueRouter);

// 解决重复定位同一路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

const router = new VueRouter({
    routes,
});

// 不重定向白名单
const whiteList = ['/login', '/agent', '/homePage', '/register'];

//全局路由前置钩子函数
router.beforeEach((to, from, next) => {
    // 判断如果是未开发的页面不让通过
    if (to.meta.isNotOpen) {
        Message.warning('暂未开放！');
        return;
    }

    // 判断是否是白名单
    if (whiteList.includes(to.path)) {
        next();
        return;
    }

    // 获取登录人请求头信息
    const TokenByUser = ys_utils.getSessionStorage('CommData');


    // store.state.__prent = 333333

    // 判断是否有用户登录信息
    if (TokenByUser) {
        //是否前去登陆页
        if (to.path == '/login') {
            // 跳转到当前页面
            next(from.fullPath);
        } else {
            //获取菜单路由图
            // let menu_ary = null
            // let menu_list = ys_utils.getSessionStorage('MenuList') ? JSON.parse(ys_utils.getSessionStorage('MenuList')) : []
            // for (let i = 0; i < menu_list.length; i++) {
            //     const itemAry = menu_list[i];
            //     if (Array.isArray(itemAry)) {
            //         for (let j = 0; j < itemAry.length; j++) {
            //             const curitem = itemAry[j];
            //             if (curitem.super_rs_code == "7402") {
            //                 menu_ary = itemAry
            //                 break
            //             } else {
            //                 break;
            //             }
            //         }
            //     }
            // }

            next();

            //递归遍历所有的菜单
            // const currentRoutes = router.options.routes
            // const newRoutes = filterNode(currentRoutes)

            // //读取有权限的路由
            // let res = newRoutes.filter((item) => {

            //     let listCon = menu_ary.map((v) => v['frame']);

            //     return listCon.includes(item['name']);
            // });

            // // 页面跳转判断是否权限
            // let keysType = res.find(function (item) { return item.name === to.name })

            // if (keysType) {
            //     next();
            // } else {
            //     Message.warning("暂未开放！");
            //     next(from.fullPath);
            // }
        }
    } else {
        next('/login');
    }
});

let newRoot = []
function filterNode(tree) {
    for (let i = 0; i < tree.length; i++) {
        const cur = tree[i];
        newRoot.push(cur)
        if (cur.children && cur.children.length > 0) {
            filterNode(cur.children)
        }
    }
    return newRoot
}
export default router;
